<template>
    <!-- 对话框表单 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :destroy-on-close="true"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '' : 'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    ref="formRef"
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                    v-if="!baTable.form.loading"
                >
                    <label id="el-id-6970-63" for="el-id-6970-77" class="el-form-item__label" style="width: 160px;margin-bottom: 15px"><h3>基础信息：</h3></label>

                    <el-form-item prop="nickname" :label="t('user.nickname')">
                        <el-input
                            v-model="baTable.form.items!.nickname"
                            type="string"
                            :placeholder="t('Please input field', { field: t('user.nickname') })"
                        ></el-input>
                    </el-form-item>
<!--                    <FormItem-->
<!--                        type="remoteSelect"-->
<!--                        :label="t('user.grouping')"-->
<!--                        v-model="baTable.form.items!.group_id"-->
<!--                        prop="group_id"-->
<!--                        :placeholder="t('user.grouping')"-->
<!--                        :input-attr="{-->
<!--                            params: { isTree: true, search: [{ field: 'status', val: '1', operator: 'eq' }] },-->
<!--                            field: 'name',-->
<!--                            'remote-url': '/admin/user.Group/index',-->
<!--                        }"-->
<!--                    />-->

                    <FormItem :label="t('user.head portrait')" type="image" v-model="baTable.form.items!.avatar" />
<!--                    <el-form-item prop="email" :label="t('user.email')">-->
<!--                        <el-input-->
<!--                            v-model="baTable.form.items!.email"-->
<!--                            type="string"-->
<!--                            :placeholder="t('Please input field', { field: t('user.email') })"-->
<!--                        ></el-input>-->
<!--                    </el-form-item>-->
                    <el-form-item prop="mobile" :label="t('user.mobile')">
                        <el-input
                            v-model="baTable.form.items!.mobile"
                            type="string"
                            :placeholder="t('Please input field', { field: t('user.mobile') })"
                        ></el-input>
                    </el-form-item>

<!--                    <el-form-item v-if="baTable.form.operate == 'Edit'" :label="t('user.integral')">-->
<!--                        <el-input v-model="baTable.form.items!.score" readonly>-->
<!--                            <template #append>-->
<!--                                <el-button @click="changeAccount('score')">{{ t('user.Adjust integral') }}</el-button>-->
<!--                            </template>-->
<!--                        </el-input>-->
<!--                    </el-form-item>-->
<!--                    <FormItem-->
<!--                        :label="t('user.is_withdraw')"-->
<!--                        type="radio"-->
<!--                        v-model="baTable.form.items!.is_withdraw"-->
<!--                        :input-attr="{ content: { 0: t('user.is_withdraw 0'), 1: t('user.is_withdraw 1') } }"-->
<!--                    />-->

                    <FormItem
                        :label="t('user.is_withdraw')"
                        v-model="baTable.form.items!.is_withdraw"
                        type="radio"
                        :data="{ content: { 0: t('user.is_withdraw 0'), 1: t('user.is_withdraw 1') }, childrenAttr: { border: true } }"
                    />
                    <FormItem
                        :label="t('State')"
                        v-model="baTable.form.items!.status"
                        type="radio"
                        :data="{ content: { disable: t('Disable'), enable: t('Enable') }, childrenAttr: { border: true } }"
                    />
                    <!-- <label id="el-id-6970-63" for="el-id-6970-77" class="el-form-item__label" style="width: 160px;margin-bottom: 15px"><h3>资金池：</h3></label><br> -->

                    <!-- <el-descriptions title=""  :column="1" style="margin-left: 100px">
                        <el-descriptions-item :label="t('goodsevaluate.goods_id')">{{baTable.form.items!.goods_id}}</el-descriptions-item>
                        <el-descriptions-item :label="t('user.coin_num')">{{baTable.form.items!.coin_num}}</el-descriptions-item>
                        <el-descriptions-item :label="t('user.coin_money')">{{baTable.form.items!.coin_money}}</el-descriptions-item>
                        <el-descriptions-item :label="t('user.back_buy_money')">{{baTable.form.items!.back_buy_money}}</el-descriptions-item>
                        <el-descriptions-item :label="t('user.directly_return_money')">{{baTable.form.items!.directly_return_money}}</el-descriptions-item>
                        <el-descriptions-item :label="t('user.push_money')">{{baTable.form.items!.push_money}}</el-descriptions-item>
                        <el-descriptions-item :label="t('user.shop_money')">{{baTable.form.items!.shop_money}}</el-descriptions-item>
                    </el-descriptions> -->



<!--                    <label id="el-id-6970-63" for="el-id-6970-77" class="el-form-item__label" style="width: 160px;margin-bottom: 15px"><h3>会员资料：</h3></label>-->


<!--                    <FormItem-->
<!--                        :label="t('user.vip_status')"-->
<!--                        v-model="baTable.form.items!.vip_status"-->
<!--                        type="radio"-->
<!--                        :data="{-->
<!--                            content: { 0: t('user.vip_status 0') , 1: t('user.vip_status 1'), },-->
<!--                            childrenAttr: { border: true },-->
<!--                        }"-->
<!--                    />-->

<!--                    <el-form-item prop="username" :label="t('user.User name')">-->
<!--                        <el-input-->
<!--                            v-model="baTable.form.items!.username"-->
<!--                            type="string"-->
<!--                            :placeholder="t('Please input field', { field: t('user.User name') })"-->
<!--                        ></el-input>-->
<!--                    </el-form-item>-->
<!--                    <FormItem :label="t('user.area')" type="city" v-model="baTable.form.items!.area" prop="area" :placeholder="t('Please select field', { field: t('user.area') })" />-->
<!--                    <FormItem-->
<!--                        :label="t('user.Gender')"-->
<!--                        v-model="baTable.form.items!.gender"-->
<!--                        type="radio"-->
<!--                        :data="{-->
<!--                            content: { 0: t('Unknown'), 1: t('user.male'), 2: t('user.female') },-->
<!--                            childrenAttr: { border: true },-->
<!--                        }"-->
<!--                    />-->
<!--                    <el-form-item :label="t('user.birthday')">-->
<!--                        <el-date-picker-->
<!--                            class="w100"-->
<!--                            value-format="YYYY-MM-DD"-->
<!--                            v-model="baTable.form.items!.birthday"-->
<!--                            type="date"-->
<!--                            :placeholder="t('Please select field', { field: t('user.birthday') })"-->
<!--                        />-->
<!--                    </el-form-item>-->


<!--                    <el-form-item prop="vip_mobile" :label="t('user.vip_mobile')">-->
<!--                        <el-input-->
<!--                            v-model="baTable.form.items!.vip_mobile"-->
<!--                            type="string"-->
<!--                            :placeholder="t('Please input field', { field: t('user.vip_mobile') })"-->
<!--                        ></el-input>-->
<!--                    </el-form-item>-->

<!--                    <el-form-item v-if="baTable.form.operate == 'Edit'" :label="t('user.balance')">-->
<!--                        <el-input v-model="baTable.form.items!.money" readonly>-->
<!--                            <template #append>-->
<!--                                <el-button @click="changeAccount('money')">{{ t('user.Adjustment balance') }}</el-button>-->
<!--                            </template>-->
<!--                        </el-input>-->
<!--                    </el-form-item>-->
<!--                    <el-form-item prop="password" :label="t('user.password')">-->
<!--                        <el-input-->
<!--                            v-model="baTable.form.items!.password"-->
<!--                            type="password"-->
<!--                            :placeholder="-->
<!--                                baTable.form.operate == 'Add'-->
<!--                                    ? t('Please input field', { field: t('user.password') })-->
<!--                                    : t('user.Please leave blank if not modified')-->
<!--                            "-->
<!--                        ></el-input>-->
<!--                    </el-form-item>-->
<!--                    <el-form-item prop="motto" :label="t('user.Personal signature')">-->
<!--                        <el-input-->
<!--                            @keyup.enter.stop=""-->
<!--                            @keyup.ctrl.enter="baTable.onSubmit(formRef)"-->
<!--                            v-model="baTable.form.items!.motto"-->
<!--                            type="textarea"-->
<!--                            :placeholder="t('Please input field', { field: t('user.Personal signature') })"-->
<!--                        ></el-input>-->
<!--                    </el-form-item>-->
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm('')">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, inject, watch } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import { regularPassword } from '/@/utils/validate'
import type { FormInstance, FormItemRule } from 'element-plus'
import FormItem from '/@/components/formItem/index.vue'
import router from '/@/router/index'
import { buildValidatorData } from '/@/utils/validate'
import { useConfig } from '/@/stores/config'

const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    // username: [buildValidatorData({ name: 'required', title: t('user.User name') }), buildValidatorData({ name: 'account' })],
    mobile: [buildValidatorData({ name: 'required', title: t('user.mobile') })],
    nickname: [buildValidatorData({ name: 'required', title: t('user.nickname') })],
    password: [
        {
            validator: (rule: any, val: string, callback: Function) => {
                if (baTable.form.operate == 'Add') {
                    if (!val) {
                        return callback(new Error(t('Please input field', { field: t('user.password') })))
                    }
                } else {
                    if (!val) {
                        return callback()
                    }
                }
                if (!regularPassword(val)) {
                    return callback(new Error(t('validate.Please enter the correct password')))
                }
                return callback()
            },
            trigger: 'blur',
        },
    ],
})

const changeAccount = (type: string) => {
    baTable.toggleForm()
    router.push({
        name: type == 'money' ? 'user/moneyLog' : 'user/scoreLog',
        query: {
            user_id: baTable.form.items!.id,
        },
    })
}

watch(
    () => baTable.form.operate,
    (newVal) => {
        rules.password![0].required = newVal == 'Add'
    }
)
</script>

<style scoped lang="scss">
.avatar-uploader {
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    border-radius: var(--el-border-radius-small);
    box-shadow: var(--el-box-shadow-light);
    border: 1px dashed var(--el-border-color);
    cursor: pointer;
    overflow: hidden;
    width: 110px;
    height: 110px;
}
.avatar-uploader:hover {
    border-color: var(--el-color-primary);
}
.avatar {
    width: 110px;
    height: 110px;
    display: block;
}
.image-slot {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
}
</style>
